package com.fengniu.ejb.dao.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.LocalBinding;
import org.jboss.ejb3.annotation.RemoteBinding;

import com.fengniu.ejb.dao.SystemDaoLocal;
import com.fengniu.ejb.dao.SystemDaoRemote;
import com.fengniu.ejb.dao.util.IEJBConstants;
import com.fengniu.ejb.entity.SystemProperty;

@Stateless
@LocalBinding(jndiBinding = IEJBConstants.SystemDaoLocalJndiName)
@RemoteBinding(jndiBinding = IEJBConstants.SystemDaoRemoteJndiName)
public class SystemDaoImpl implements SystemDaoLocal, SystemDaoRemote {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8430575944268213859L;

	@PersistenceContext(unitName = "default")
	protected EntityManager em;

	public SystemProperty save(SystemProperty systemProperty) {
		SystemProperty sp = em.merge(systemProperty);
		return sp;
	}

	public SystemProperty findSystemPropertyBy(String propertyId) {
		SystemProperty result = null;
		Query q = em.createQuery("select u from SystemProperty u where u.propertyId=:propertyId").setParameter(
				"propertyId", propertyId).setMaxResults(1);
		List<SystemProperty> sps = q.getResultList();
		if (sps != null && sps.size() > 0) {
			result = sps.get(0);
		}
		return result;
	}

	public SystemProperty findSystemPropertyBy(long sysId) {
		return em.find(SystemProperty.class, sysId);
	}

	public List<SystemProperty> getAllSystemProperty() {
		Query q = em.createQuery("select u from SystemProperty u");
		List<SystemProperty> sps = q.getResultList();
		return sps;
	}

	public void cleanDataBase() {
		Query q;
		q = em.createQuery("delete from Content2Content");
		q.executeUpdate();
		q = em.createQuery("delete from Site2Site");
		q.executeUpdate();
		q = em.createQuery("delete from User2User");
		q.executeUpdate();
		q = em.createQuery("delete from Category2Category");
		q.executeUpdate();
		q = em.createQuery("delete from Site2Category");
		q.executeUpdate();
		q = em.createQuery("delete from Content2Category");
		q.executeUpdate();
		q = em.createQuery("delete from User2Category");
		q.executeUpdate();

		q = em.createQuery("delete from User2SiteOrderItem");
		q.executeUpdate();
		q = em.createQuery("delete from User2SiteOrder");
		q.executeUpdate();

		q = em.createQuery("delete from User2Site");
		q.executeUpdate();
		q = em.createQuery("delete from User2Content");
		q.executeUpdate();
		q = em.createQuery("delete from Site2Content");
		q.executeUpdate();

		q = em.createQuery("delete from Content");
		q.executeUpdate();
		q = em.createQuery("delete from Contact");
		q.executeUpdate();
		q = em.createQuery("delete from Site");
		q.executeUpdate();
		q = em.createQuery("delete from User where not(emailAddr='conan8chan@yahoo.com')");
		q.executeUpdate();
		q = em.createQuery("update Category set primaryParent=null");
		q.executeUpdate();
		q = em.createQuery("delete from Category");
		q.executeUpdate();

	}

}
